package com.luke.session_mvc.service;

import com.luke.session_mvc.model.AuthenticationRequest;
import com.luke.session_mvc.model.UserDto;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@Service
public class AuthenticationServiceImpl implements AuthenticationService {

    @Override
    public UserDto authentication(AuthenticationRequest request) {
        if (request == null || StringUtils.isEmpty(request.getUsername()) || StringUtils.isEmpty(request.getPassword())) {
            throw new RuntimeException("账号或密码为空");
        }
        UserDto userDto = getUserDto(request.getUsername());
        if (userDto == null) {
            throw new RuntimeException("查询不到该用户");
        }
        if (!request.getPassword().equals(userDto.getPassword())) {
            throw new RuntimeException("账号或密码错误");
        }
        return userDto;
    }

    //模拟用户查询
    public UserDto getUserDto(String username) {
        return userMap.get(username);
    }

    //用户信息
    private Map<String, UserDto> userMap = new HashMap<>();
    {
        //权限
        Set<String> authorities1 = new HashSet<>();
        authorities1.add("p1");
        Set<String> authorities2 = new HashSet<>();
        authorities2.add("p2");
        //用户
        userMap.put("zhangsan", new UserDto("1010", "zhangsan", "123", "张三", "133443",authorities1));
        userMap.put("lisi", new UserDto("1011", "lisi", "456", "李四", "144553",authorities2));
    }

}
